package com.azkj.album.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.azkj.album.R;
import com.azkj.album.adapter.BackGroundListAdapter;
import com.azkj.album.dto.AddOrderBean;
import com.azkj.album.dto.BackGroundListBean;
import com.azkj.album.dto.ListData;
import com.azkj.album.dto.MessageEvent;
import com.azkj.album.dto.MessageType;
import com.azkj.album.dto.NewGalleryBean;
import com.azkj.album.dto.NewPhotoBean;
import com.azkj.album.dto.VipInfoBean;
import com.azkj.album.network.Constants;
import com.azkj.album.network.utils.ToastUtils;
import com.azkj.album.presenter.NewPhotoPresenter;
import com.azkj.album.utils.Utils;
import com.azkj.album.view.base.BaseActivity;
import com.azkj.album.view.iview.INewPhotoView;
import com.azkj.album.view.widgets.staggedrv.GridItemDecoration;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class NewPhotoBackgroundActivity extends BaseActivity implements INewPhotoView {
    @BindView(R.id.tv_photo_class_layout)
    RelativeLayout mPhotoClassLayout;
    @BindView(R.id.rv_class_photo_layout)
    RecyclerView mRvPhotoClass;
    @BindView(R.id.tv_backgronnd_class)
    TextView mTvBackGround;
    @BindView(R.id.tv_new_photo_select_next)
    TextView tvNext;
    private BackGroundListAdapter backGroundListAdapter;
    private List<NewGalleryBean> newGalleryBeans = new ArrayList<>();
    private List<NewPhotoBean> newBackGround = new ArrayList<>();
    private List<String> backgroundName = new ArrayList<>();
    private ArrayList<String> imagesSelect = new ArrayList<>();
    private ArrayList<String> imagesAll = new ArrayList<>();

    //新建图册选择的分类ID
    private Integer selectClassID;
    private Integer selectBackGroundID;
    private BackGroundListBean selectBackGroundBean;

    private NewPhotoPresenter mPresenter;
    private int type;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_new_photo_background;
    }

    @Override
    public void initView() {
        Intent data = getIntent();
        imagesSelect = (ArrayList<String>) data.getSerializableExtra("imagesSelect");
        imagesAll = (ArrayList<String>) data.getSerializableExtra("imagesAll");
        selectClassID = data.getIntExtra("selectClassID", 0);
        type = data.getIntExtra("type", -1);
        tvNext.setText(type == 2 ? "确定" : "下一步");
    }

    @Override
    public void initData() {
        mPresenter.selectBackClass();
        mPresenter.selectBackList("0", "1", "40");
        backGroundListAdapter = new BackGroundListAdapter(NewPhotoBackgroundActivity.this, R.layout.activity_add_background_select);
        mRvPhotoClass.setLayoutManager(new GridLayoutManager((Context) this, 2));
        mRvPhotoClass.addItemDecoration(new GridItemDecoration(this, 5));
        mRvPhotoClass.setAdapter(backGroundListAdapter);

        backGroundListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < backGroundListAdapter.getData().size(); i++) {
                    if (i == position) {

                        boolean select = backGroundListAdapter.getData().get(i).isSelect();
                        if (select){
                            selectBackGroundBean = null;
                        }else {
                            selectBackGroundBean = backGroundListAdapter.getData().get(i);
                        }
                        backGroundListAdapter.getData().get(i).setSelect(!select);
                    } else {
                        backGroundListAdapter.getData().get(i).setSelect(false);
                    }
                    backGroundListAdapter.notifyDataSetChanged();
                }
            }
        });
//        backGroundListAdapter.setItemOnClickInterface(new BackGroundListAdapter.ItemOnClickInterface() {
//            @Override
//            public void onItemClick(int postation) {
//
//            }
//        });
    }

    @OnClick({R.id.iv_back, R.id.tv_new_photo_select_next, R.id.tv_backgronnd_class})
    public void onClick(View v) {
        if (Utils.isFastClick()) return;
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_new_photo_select_next:
                if (null == selectBackGroundBean) {
                    ToastUtils.showCenterToast("请选择背景图片");
                    return;
                }
                Intent next = new Intent(NewPhotoBackgroundActivity.this, NewPhotoSynthesisActivity.class);
                next.putStringArrayListExtra("imagesSelect", imagesSelect);
                next.putExtra("selectClassID", selectClassID);
                next.putExtra("selectBackGroundID", selectBackGroundID);
                next.putExtra("selectBackGroundBean", selectBackGroundBean);
                next.putStringArrayListExtra("imagesAll", imagesAll);
                // 携带type和detail
                next.putExtra("detail", getIntent().getSerializableExtra("detail"));
                next.putExtra("type", getIntent().getIntExtra("type", -1));
                if (type == 2){
                    setResult(RESULT_OK, next);
                    finish();
                }else {
                    startActivity(next);
                }
                break;
            case R.id.tv_backgronnd_class:
                OptionsPickerView<String> pickerView = new OptionsPickerBuilder((Context) this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        // 处理选择结果
                        String result = newBackGround.get(options1).getName();
                        selectBackGroundID = newBackGround.get(options1).getId();
                        mTvBackGround.setText(result);
                        mPresenter.selectBackList(String.valueOf(selectBackGroundID), "1", "20");
                    }
                }).build();
                // 填充数据源
                pickerView.setPicker(backgroundName);
                pickerView.setSelectOptions(0);
                pickerView.show();
                break;
        }
    }

    public void onMessage(MessageEvent messageEvent) {
        super.onMessage(messageEvent);
        switch (messageEvent.getType()) {
            case MessageType.TYPE_ON_SAVE_SUCCESS:
                finish();
                break;
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter = new NewPhotoPresenter(this);
    }

    @Override
    public void classSuccess(List<NewPhotoBean> newPhotoBeans) {

    }

    @Override
    public void classFail(String msg) {

    }

    @Override
    public void uploadFileSuccess(int position, NewGalleryBean bean, boolean isCompile, int saveCount) {

    }

    @Override
    public void uploadFileFail(String msg) {

    }

    @Override
    public void uploadProcess(int position, NewGalleryBean bean, boolean isCompile) {

    }

    @Override
    public void newSaveSuccess(String stringSave) {

    }

    @Override
    public void newSaveFail(String msg) {

    }

    @Override
    public void backGroundSuccess(List<NewPhotoBean> stringBackGround) {
        backgroundName.add("全部");
        mTvBackGround.setText("全部");
        for (int i = 0; i < stringBackGround.size(); i++) {
            backgroundName.add(stringBackGround.get(i).getName());
        }
        NewPhotoBean newPhotoBean = new NewPhotoBean();
        newPhotoBean.setId(0);
        newPhotoBean.setName("全部");
        stringBackGround.add(0,newPhotoBean);
        newBackGround.addAll(stringBackGround);
    }

    @Override
    public void backGroundFail(String msg) {

    }

    @Override
    public void backGroundListSuccess(List<BackGroundListBean> stringBackGround) {
        backGroundListAdapter.setNewData(stringBackGround);
    }

    @Override
    public void backGroundListFail(String msg) {

    }

    @Override
    public void introSuccess(List<String> stringHistoryList) {

    }

    @Override
    public void introFail(String msg) {

    }

    @Override
    public void remarkSuccess(List<String> stringRemarkList) {

    }

    @Override
    public void remarkFail(String msg) {

    }

    @Override
    public void addOrderSuccess(AddOrderBean addOrderBeans) {

    }

    @Override
    public void addOrderFail(String msg) {

    }

    @Override
    public void getPerDetail(ListData listData) {

    }
    @Override
    public void getPerDetailFail(String msg) {

    }

    @Override
    public void VipSuccess(VipInfoBean vipInfoBean) {

    }

    @Override
    public void VipFail(String error) {

    }

    @Override
    public void deleteIntroSuccess(String sString) {

    }

    @Override
    public void deleteIntroFail(String error) {

    }

    @Override
    public void deleteRemarkSuccess(String sString) {

    }

    @Override
    public void deleteRemarkFail(String error) {

    }
}
